package com.jd.jobkeeper.server.web.controller;


import com.jd.jobkeeper.server.service.ServerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("address")
public class AddressIndexController {

    @Autowired
    ServerInfoService serverInfoService;

    @GetMapping
    public String address() {
        StringBuilder result = new StringBuilder();
        long c = System.currentTimeMillis();
        long h = serverInfoService.getHealthThreshold();
        serverInfoService.findAll().stream()
                .filter(s-> (c - s.getHeartBeatTime().getTime()) <= h)
                .map(s-> s.netAddress())
                .forEach(s-> result.append(s).append(','));
        return result.toString();
    }
}
